using System;
using System.Collections.Generic;
using Orchard;
using Orchard.ContentManagement;
using Orchard.Mvc;
using Orchard.Widgets.Services;

namespace So.WidgetThemeLayers
{
    public class ThemeRuleProvider : IRuleProvider {
		private readonly IHttpContextAccessor _httpContextAccessor;
		private readonly IWorkContextAccessor _workContextAccessor;

        public ThemeRuleProvider(IHttpContextAccessor httpContextAccessor, IWorkContextAccessor workContextAccessor)
        {
		    _httpContextAccessor = httpContextAccessor;
		    _workContextAccessor = workContextAccessor;
		}

        public void Process(RuleContext ruleContext) {
            if (!String.Equals(ruleContext.FunctionName, "theme", StringComparison.OrdinalIgnoreCase) ||
                ruleContext.Arguments[0] == null ||
                String.IsNullOrWhiteSpace(ruleContext.Arguments[0].ToString()))
            {
                return;
            }

            ruleContext.Result = false;
            var typeParam = ruleContext.Arguments[0].ToString();
            if(_workContextAccessor.GetContext().CurrentTheme.Name.Equals(typeParam,StringComparison.CurrentCultureIgnoreCase))
                ruleContext.Result = true;
        }
    }
}